package com.example.drools.spring.yc.service.impl;

import com.example.drools.spring.yc.entity.YcEnterpriseEntity;
import com.example.drools.spring.yc.entity.YcUniqueEntity;
import com.example.drools.spring.yc.service.YcUniqueCacheService;
import com.example.drools.spring.yc.util.BeanContextUtil;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ddsyw
 * @version 1.0
 * @date 2021/12/31 1:47 下午
 */
@Slf4j
public class YcEnterpriseUniqueServiceImpl {

  private static final String BEAN_NAME = "ycEnterpriseUniqueCacheService";

  public static void checkUnique(YcUniqueEntity ycUniqueEntity) {
    if (Objects.isNull(ycUniqueEntity)) {
      return;
    }
    YcEnterpriseEntity ycEnterpriseEntity = (YcEnterpriseEntity) ycUniqueEntity;
    checkData(ycEnterpriseEntity);
  }

  private static void checkData(YcEnterpriseEntity ycEnterpriseEntity) {
    if (Objects.nonNull(ycEnterpriseEntity.getPermitNo()) && getBean()
        .checkUnique(ycEnterpriseEntity)) {
      ycEnterpriseEntity.setUnique(true);
    }
  }

  private static YcUniqueCacheService getBean() {
    return BeanContextUtil.getBean(BEAN_NAME);
  }
}
